package quickup;

import java.util.ArrayList;

import javax.swing.JProgressBar;
import javax.swing.table.AbstractTableModel;

public class FileUploadTableModel extends AbstractTableModel {
	
	private static final long serialVersionUID = 1L;
	private static final String[] columnNames = {"", "File", "Size", "Progress", "Status"};
	private ArrayList<FileUpload> files = null;	

	public FileUploadTableModel() {
		files = new ArrayList<FileUpload>();
	}
	
	public void add(FileUpload fu) {
		files.add(fu);
		fireTableRowsInserted(files.size()-1,files.size()-1);
	}
	
	public void clear() {
		files.clear();
		this.fireTableDataChanged();
	}
	
	public int getColumnCount() {
		return columnNames.length;
	}

	public int getRowCount() {
		return files.size();
	}

	public String getColumnName(int col) {
		return columnNames[col];
	}

	public Object getValueAt(int row, int col) {
		if(row < files.size());
		switch(col) {
			case 0: return new Boolean(files.get(row).isEnabled());
			case 1: return files.get(row).getFileName();
			case 2: return "" + files.get(row).getFileSize() + "kb";
			case 3: return files.get(row).getProgressBar();
			case 4: return files.get(row).getStatus();
		} return null;
	}

	public Class<?> getColumnClass(int c) {
		switch(c) {
			case 0: return Boolean.class;
			case 3: return JProgressBar.class;
			default: return String.class;
		}
	}
	
	public boolean isCellEditable(int row, int col) {
		return (col == 0);
	}

	public void setValueAt(Object value, int row, int col) {
		files.get(row).setEnabled((Boolean)value);
		fireTableCellUpdated(row, 0);
	}

	public ArrayList<FileUpload> getFileUploads() {
		ArrayList<FileUpload> selected = new ArrayList<FileUpload>(files);
		int missing = 0;
		for(int r = 0; r < files.size(); r++)
			if(!((Boolean)getValueAt(r,0)).booleanValue()) {
				selected.remove(r - missing++); 
			}
		return selected;
	}
}
